package com.xxj.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;

import com.xxj.entity.MessageEntity;
import com.xxj.entity.UserEntity;
import com.xxj.util.DBManager;
import com.xxj.util.QueryResult;

public class UserDaoImpl {
	public UserEntity login(String name) {
		UserEntity userEntity = null;
		String sqlString = "select id,create_time,name,pwd from user where name = '"+name+"'";
		QueryResult result = DBManager.executeQuery(sqlString);
		if(result!=null) {
			try {
				while(result.rs.next()) {
					userEntity = new UserEntity(
							result.rs.getInt("id"), 
							result.rs.getString("create_time"),
							result.rs.getString("name"),
							result.rs.getString("pwd"));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		result.close();
		return userEntity;
	}
	
	public ArrayList<UserEntity> user() {
		ArrayList<UserEntity> aList = new ArrayList<UserEntity>();
		String sqlString = "select id,create_time,name,pwd from user";
		QueryResult result = DBManager.executeQuery(sqlString);
		if(result!=null) {
			try {
				while(result.rs.next()) {
					aList.add(new UserEntity(result.rs.getInt("id"), 
							result.rs.getString("create_time"),
							result.rs.getString("name"),
							result.rs.getString("pwd")));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		result.close();
		return aList;
	}
	
	public int userAdd(UserEntity user) {
		String sqlString = "insert into user(name pwd) values('"+user.getName()+"','"+user.getPwd()+"')";
		int result = DBManager.executeUpdate(sqlString);
		return result;
	}
	
	public int userUpdate(UserEntity user) {
		String sqlString = "update user set name='"+user.getName()+"',pwd='"+user.getPwd()+"'";
		int result = DBManager.executeUpdate(sqlString);
		return result;
	}
	
	public int userDelete(int id) {
		String sqlString = "delete from user where id = '"+id+"'";
		int result = DBManager.executeUpdate(sqlString);
		return result;
	}

}
